package com.wze.wze.Controller;

import com.wze.wze.Aop.PasToken;
import com.wze.wze.Entity.LikeMusic;
import com.wze.wze.Entity.ResultEntity;
import com.wze.wze.Entity.UserTab;
import com.wze.wze.Service.LikeMusicService;
import com.wze.wze.Util.JSONTool;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin //解决跨域
@RestController //控制器注解
@RequestMapping("/lmusic/")//请求路径
public class LikeMusicController {
    @Resource
    LikeMusicService likeMusicService;
    ResultEntity resultEntity = null;

    @PasToken
    @PutMapping("lmusic")
    private String upLikeMusic(@RequestParam("music") String newmusic, @RequestParam("uid") int uid) {
        resultEntity = new ResultEntity();
        try {
            String music="";
            LikeMusic likeMusic = new LikeMusic();
            likeMusic.setMusic(music);
            likeMusic.setUid(uid);
            if (likeMusicService.upLikeMusic(likeMusic) > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PostMapping("lmusic")
    private String addLikeMusic(@RequestParam("music") String music, @RequestParam("uid") int uid, @RequestParam("mid") int mid) {
        resultEntity = new ResultEntity();
        try {
            if (likeMusicService.addLikeMusic(music,uid,mid) > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @GetMapping("UNlmusic")
    private String uniaddLikeMusic(@RequestParam("music") String music, @RequestParam("uid") int uid, @RequestParam("mid") int mid) {
        resultEntity = new ResultEntity();
        try {
            if (likeMusicService.addLikeMusic(music,uid,mid) > 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @PasToken
    @GetMapping("lmusicList")
    private String LikeMusicList(
            @RequestParam("page") int page,
            @RequestParam("limit") int limit,
            @RequestParam("mid") int mid,
            @RequestParam("uid") int uid
    ){
        try {
            resultEntity = new ResultEntity();
            int Start = (page-1) * limit;//计算分页位置
            List<LikeMusic> list = likeMusicService.getLikeMusicList(Start,limit,uid,mid);
                resultEntity.setCode("0");
            resultEntity.setData(list);
            resultEntity.setMsg("查询成功!");
        }catch (Exception e){
            resultEntity.setCode("1");
            resultEntity.setMsg("查询失败!"+e.getMessage());
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    @GetMapping("lMusic")
    private String getUserById(@RequestParam("uid") int uid,@RequestParam("mid") int mid) {
        resultEntity = new ResultEntity();
        try {
            LikeMusic likeMusic = likeMusicService.getMusicByuid(uid, mid);
            if (likeMusic == null) {
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            } else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(likeMusic);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);

    }

    @DeleteMapping("lmusic")
    private String delUser(@RequestParam("uid") int uid,@RequestParam("mid") int mid) {
        resultEntity = new ResultEntity();
        try {
            if (likeMusicService.delLikeMusicByuId(uid,mid)> 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }


    @GetMapping("UNdellmusic")
    private String delUnUser(@RequestParam("uid") int uid,@RequestParam("mid") int mid) {
        resultEntity = new ResultEntity();
        try {
            if (likeMusicService.delLikeMusicByuId(uid,mid)> 0) {
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }




}
